﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class WeaponPool : MonoBehaviour{

	private Stack<GameObject> objectStack;
	private Stack<GameObject> tempObjectStack;
	
	private Action<GameObject> resetAction;
	private Action<GameObject> oneTimeInitAction;

	private string weaponTypName;
	private GameObject gameObj;
	private int i;//test


	public WeaponPool(int stackCount, GameObject gamePrefab ,Action<GameObject> resetAction = null, Action<GameObject> oneTimeInitAction = null)
	{
		objectStack = new Stack<GameObject>();
		tempObjectStack = new Stack<GameObject>();	
		this.resetAction = resetAction;
		this.oneTimeInitAction = oneTimeInitAction;
		gameObj = gamePrefab;
		// Init(stackCount);
	}

	// 初始化池里面对象
	// public bool Init(int stackCount)
	// {
	// 	for(int i =0;i<stackCount;i++)
	// 	{
	// 		GameObject info = GameObject.Instantiate(gameObj);
	// 		if(oneTimeInitAction != null)
	// 		{
	// 			oneTimeInitAction(info);
	// 		}
	// 		objectStack.Push(info);
	// 	}
	// 	print(objectStack.Count);
	// 	return true;
	// }
	void FixedUpdate()
	{

	}

	// 取对象
	public GameObject New()
	{
		if(objectStack.Count > 0)
		{
			GameObject info = objectStack.Pop();
			if(resetAction != null)
			{
				resetAction(info);
			}
			return info;
		}
		else{
			GameObject info = GameObject.Instantiate(gameObj);
			if(oneTimeInitAction != null)
			{
				oneTimeInitAction(info);
			}
			return info;
		}
	}

	// 丢进池
	public void Store(GameObject info)
	{
//		objectStack.Push(info);
		tempObjectStack.Push(info);
		if (tempObjectStack.Count > 3) 
		{
			GameObject tempInfo = tempObjectStack.Pop();
			objectStack.Push(tempInfo);
		}
	}
		
	
	// public void setWeaponTypeName(string weaponTypName, GameObject gameObj)
	// {
	// 	this.weaponTypName = weaponTypName;
	// 	// this.gameObj = gameObj;
	// }

}
